package com.webserver.controller;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理与文章相关的业务
 */
public class ArticleController {

    public static final String ARTICLE_DIR = "./articles/";

    static {
        File dir = new File(ARTICLE_DIR);
        if(!dir.exists()) {//如果该目录不存在则自动创建
            dir.mkdirs();
        }
    }

    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理发表文章....");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        if(title==null||author==null||content==null){
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }

        try (
                FileOutputStream fos = new FileOutputStream(ARTICLE_DIR+title+".obj");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            Article article = new Article(title,author,content);
            oos.writeObject(article);
            File file = new File("./webapps/myweb/article_success.html");
            response.setEntity(file);
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println("发表文章处理完毕!");
    }
    public void showAllArticle(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理动态页面");
        //1 将ariticles目录中的所有用户读取回来
        List<Article> ariList = new ArrayList<>();
        //扫描users目录中的所有.obj文件，并将每个文件中的User对象反序列化回来后存入userList
        File dir = new File(ARTICLE_DIR);
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File ariFile : subs) {
            try (
                    FileInputStream fis = new FileInputStream(ariFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Article article = (Article) ois.readObject();
                ariList.add(article);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(ariList);
        //2 将所有用户拼接到html代码中并生成页面
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang = \"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println(" <title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println(" <td>作者</td>");
        for (Article article : ariList) {
            pw.println("<tr>");
            pw.println("<td><a href=\"/myweb/showArticle?title=文章标题\">"
                    + article.getTitle() + "</a></td>");
            pw.println("<td>" + article.getAuthor() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</table> ");
        pw.println("</form> ");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");


        //3 设置响应头Content - Type,告知浏览器这些动态数据是一个页面
        response.setContentType("text/html");
        System.out.println("动态页面处理完毕");
    }
}
